﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfileSubtypeActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The profile subtype actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Profiles
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The profile subtype actioner.
    /// </summary>
    internal class ProfileSubtypeActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProfileSubtypeActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public ProfileSubtypeActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create profile subtype.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="pType">
        /// The p type.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateProfileSubtype(string name, ProfileType pType)
        {
            var type = new ProfileSubtype
                           {
                               ID = Guid.NewGuid(),
                               Name = name,
                               Type = pType,
                               CreatedDateTime = DateTime.Now
                           };
            this.Session.Save(type);

            this.Log.CreateLogEntry(
                type.ID, string.Format("Profile relation '{0}', blev oprettet.", type.Name), Enums.LogEntryType.ProfileSubtype);

            return type.ID;
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileSubtypesResult"/>.
        /// </returns>
        public ProfileSubtypesResult ExecuteSearchQuery(ProfileSubtypesQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new ProfileSubtypesResult();

            IQueryOver<ProfileSubtype, ProfileSubtype> search = this.Session.QueryOver<ProfileSubtype>();
            this.ApplyDefaultCriterias(settings, search);

            result.TotalRowCount = search.RowCount();

            var types = this.ApplySkipAndTake(settings, search.OrderBy(x => x.Name).Asc);

            foreach (ProfileSubtype profileSubtype in types)
            {
                Services.Dto.ProfileSubtype dto = Services.Dto.ProfileSubtype.Convert(profileSubtype);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get profile subtype.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileSubtype"/>.
        /// </returns>
        public ProfileSubtype GetProfileSubtype(Guid id)
        {
            var type = this.Session.Get<ProfileSubtype>(id);

            return type;
        }

        #endregion
    }
}